import numpy as np
from simple_tif import tif_to_array, array_to_tif

im_1 = tif_to_array('angle_reorganized.tif').astype(np.float64)
im_1 = im_1.reshape(3, 5, 55, 128, 128)
im_1= im_1.max(axis = 2)
im_1=im_1.reshape(15, 128, 128)
array_to_tif(im_1.astype(np.float32), 'composite_1.tif')
amp_1 = np.ones_like(im_1)
ph_1 = np.ones_like(im_1)

for i in range(im_1.shape[0]):
    amp_1[i, :, :] = np.log(np.abs(np.fft.fftshift(np.fft.fftn(im_1[i, :, :]))))
    ph_1[i, :, :] = np.angle(np.fft.fftshift(np.fft.fftn(im_1[i, :, :])))
                            
array_to_tif(amp_1.astype(np.float32), 'amp_1.tif')
array_to_tif(ph_1.astype(np.float32), 'ph_1.tif')

im_2 = tif_to_array('noiseless_sim_data.tif').astype(np.float64)
im_2 = im_2.reshape(3, 5, 55, 128, 128)
im_2= im_2.max(axis = 2)
im_2=im_2.reshape(15, 128, 128)
array_to_tif(im_2.astype(np.float32), 'composite_2.tif')
amp_2 = np.ones_like(im_2)
ph_2 = np.ones_like(im_2)

for i in range(im_2.shape[0]):
    amp_2[i, :, :] = np.log(np.abs(np.fft.fftshift(np.fft.fftn(im_2[i, :, :]))))
    ph_2[i, :, :] = np.angle(np.fft.fftshift(np.fft.fftn(im_2[i, :, :])))
                            
array_to_tif(amp_2.astype(np.float32), 'amp_2.tif')
array_to_tif(ph_2.astype(np.float32), 'ph_2.tif')

locs = [
    (0, 29, 29),
    (1, 29, 29),
    (2, 29, 29),
    (3, 29, 29),
    (4, 29, 29),
    (5, 76, 17),
    (6, 76, 17),
    (7, 76, 17),
    (8, 76, 17),
    (9, 76, 17),
    (10, 17, 77),
    (11, 17, 77),
    (12, 17, 77),
    (13, 17, 77),
    (14, 17, 77),
    ]
print 'Simulated'
##for i, p in enumerate(ph_1_locs):
##    print i, ":", ph_1[p]

print 'real'
##for i, p in enumerate(ph_2_locs):
##    print i, ":", ph_2[p]

print '\nsimulated-real'

for x in locs:
    print np.sqrt((x[1] - 64)**2 + (x[2] - 64)**2)

for o in range(3):
    print
    dif_1 = ph_1[locs[5*o + 4]] - ph_1[locs[5*o]]
    dif_2 = ph_2[locs[5*o + 4]] - ph_2[locs[5*o]]
    print dif_1, dif_2
    while dif_1 < 0:
        dif_1 += 2*np.pi
    while dif_1 > 2*np.pi:
        dif_1 -= np.pi
    while dif_2 < 0:
        dif_2 += 2*np.pi
    while dif_2 > 2*np.pi:
        dif_2 -= np.pi
    print "Orientation", o, "4 phase steps:", dif_1 / (0.4*2*np.pi)
    print "Orientation", o, "4 phase steps:", dif_2 / (0.4*2*np.pi)
print

for o in range(3):
    print "Orientation", o
    total_dif_1 = 0
    total_dif_2 = 0
    for p in range(4):
        dif_1 = ph_1[locs[5*o + p + 1]] - ph_1[locs[5*o + p]]
        dif_2 = ph_2[locs[5*o + p + 1]] - ph_2[locs[5*o + p]]
        print "     ", dif_1, dif_2
        while dif_1 < 0:
            dif_1 += 2*np.pi
        while dif_1 > 2*np.pi:
            dif_1 -= np.pi
        while dif_2 < 0:
            dif_2 += 2*np.pi
        while dif_2 > 2*np.pi:
            dif_2 -= np.pi
        print "    ", dif_1, dif_2
        total_dif_1 += dif_1
        total_dif_2 += dif_2
        print " Phase step", p, ":", dif_1 / (0.6*2*np.pi)
        print " Phase step", p, ":", dif_2 / (0.6*2*np.pi)
    print "Total dif", o, ":", total_dif_1 / (2.4*2*np.pi)
    print "Total dif", o, ":", total_dif_2 / (2.4*2*np.pi)
    print
print


for i in range(len(locs)):
##    print "Amplitudes:", amp_1[locs[i]], amp_2[locs[i]]
    dif = ph_1[locs[i]] - ph_2[locs[i]]
    while dif < -np.pi:
        dif = dif + 2*np.pi
    while dif > np.pi:
        dif = dif - 2*np.pi
    print i, ":", dif

for o in range(3):
    avg = 0
    for p in range(5):
        i = 5 * o + p
        dif = ph_1[locs[i]] - ph_2[locs[i]]
        while dif < -np.pi:
            dif = dif + 2*np.pi
        while dif > np.pi:
            dif = dif - 2*np.pi
        avg += dif * 0.2
    print "Orientation", o, ":", avg, "average"
        

